<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
  }
  .product-analysis-form {
    margin-bottom: -10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
  .product-analysis-btn-export {
    margin-bottom: 10px;
    /* margin-top: -46px; */
    /* float: right; */
  }
  .sale-tag {
    width: 180px;
    height: 50px;
    font-size: 13px;
    text-align: center;
    line-height: 50px;
    margin-left: 3px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>产品分析</span>
    </div>
    <div class="line"></div>
    <sale-analysis-form
      class="product-analysis-form"
      @on-success-valid="handleSubmit">
    </sale-analysis-form>
    <div>
      <Tabs type="card" style="width: 100%" @on-click="handleClickTab">
        <TabPane label="列表" name="列表">
          <btn-export :tableData="productAnalysisList" :tableTitle="columns" :title="title" class="product-analysis-btn-export"></btn-export>
          <Table
            class="table"
            :columns="columns"
            :data="historyData"
            :loading="loading"
            border>
          </Table>
          <table-page
            class="paging"
            :dataCount="dataCount"
            @change-page="changePage"
            @change-page-size="changePageSize"
            :pageSize="pageSize">
          </table-page>
        </TabPane>
        <TabPane label="成交量排名" name="成交量排名">
          <Button type="primary" ghost @click="handleSellOrder">爆品产品</Button>
          <Button type="primary" ghost style="margin-left: 5px;" @click="handleSellOrderDec">销量低产品</Button>
          <Pie v-if="showPie" style="height: 800px" :text='sellText' :value="sellOrderList" :seriesName='seriesName' />
        </TabPane>
        <TabPane label="流失量排名" name="流失量排名">
          <!-- <Button type="primary" ghost @click="handleLostOrder">爆品产品</Button>
          <Button type="primary" ghost style="margin-left: 5px;" @click="handleLostOrderDec">销量低产品</Button> -->
          <Pie v-if="showPie" style="height: 800px" :text='lostText' :value="lostOrderList" :seriesName='seriesName' />
        </TabPane>
        <TabPane label="意向量排名" name="意向量排名">
          <Pie v-if="showPie" style="height: 800px" :text='wantText' :value="wantOrderList" :seriesName='seriesName' />
        </TabPane>
        <TabPane label="成交率排名" name="成交率排名">
          <rate-bar v-if="showBar" style="height:600px" :value="orderRateList" :name="saleProductName" :color="productRankColor" :text="orderRateText" />
        </TabPane>
        <TabPane label="流失率排名" name="流失率排名">
          <rate-bar v-if="showBar" style="height:600px" :value="lostRateList" :name="lostName" :color="lostRankColor" :text="lostRateText" />
        </TabPane>
        <TabPane label="销售业绩排名" name="销售业绩排名">
          <Button type="primary" ghost @click="handleSellMoneyOrder">销售业绩高</Button>
          <Button type="primary" ghost style="margin-left: 5px;" @click="handleSellMoneyOrderDec">销售业绩低</Button>
          <Pie v-if="showPie" style="height: 800px" :text='saleText' :value="sellMoneyOrderList" :seriesName='seriesName' />
        </TabPane>
      </Tabs>
    </div>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import SaleAnalysisForm from '_c/sale-analysis-form'
import BtnExport from '_c/btn-export'
import Pie from '_c/pie'
import RateBar from '_c/rate-bar'
import TablePage from '_c/table-page'
export default {
  components: {
    SaleAnalysisForm,
    TablePage,
    Pie,
    RateBar,
    BtnExport
  },
  data () {
    return {
      page: 1,
      title: '销售管理产品分析',
      rows: 20,
      lostText: '流失产品排名',
      sellText: '爆品产品',
      wantText: '意向产品排名',
      orderRateText: '成交率排名',
      lostRateText: '流失率排名',
      saleText: '销售业绩高',
      saleProductName: '成交率',
      lostName: '流失率',
      productRankColor: 'orange',
      lostRankColor: 'rgba(44, 132, 248, 0.945)',
      confirmData: {},
      postData: {},
      playload: null,
      loading: true,
      showPie: false,
      showBar: false,
      seriesName: '',
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 100,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        // {
        //   title: '日期',
        //   key: 'ctime',
        //   minWidth: 150,
        //   align: 'center',
        //   sortable: true
        // },
        {
          title: '商品',
          key: 'gname',
          minWidth: 130,
          align: 'center',
          render: (h, params) => {
            if (params.row.gname && params.row.gname.length > 5) {
              return h('span', {
                attrs: {
                  title: params.row.gname
                }
              }, params.row.gname ? params.row.gname.slice(0, 5) + '...' : '-')
            } else {
              return h('span', params.row.gname ? params.row.gname : '-')
            }
          }
        },
        {
          title: '成交客户',
          key: 'cstnum',
          minWidth: 150,
          align: 'center',
          sortable: true
        },
        {
          title: '流失客户',
          key: 'lostnum',
          minWidth: 150,
          align: 'center',
          sortable: true
        },
        {
          title: '意向客户',
          key: 'allnum',
          minWidth: 150,
          align: 'center',
          sortable: true
        },
        {
          title: '成交率',
          key: 'yxrate',
          minWidth: 150,
          align: 'center',
          sortable: true,
          render: (h, params) => {
            return h('span', `${params.row.yxrate}%`)
          }
        },
        {
          title: '流失率',
          key: 'lostrate',
          minWidth: 150,
          align: 'center',
          sortable: true,
          render: (h, params) => {
            return h('span', `${params.row.lostrate}%`)
          }
        },
        {
          title: '销售金额',
          key: 'salemoney',
          minWidth: 150,
          align: 'center',
          sortable: true
        }
        // {
        //   title: '操作',
        //   key: 'cz',
        //   // width: 200,
        //   align: 'center',
        //   render: (h, params) => {
        //     return h('div', [
        //       h('Button', {
        //         props: {
        //           type: 'primary',
        //           size: 'small',
        //           ghost: 'ghost'
        //         },
        //         style: {
        //           marginRight: '5px'
        //         },
        //         on: {
        //           click: () => {
        //             this.handleClick(params.row.id, params.row)
        //           }
        //         }
        //       }, '详情')
        //     ])
        //   }
        // }
      ]
    }
  },
  computed: {
    ...mapState({
      productAnalysisList: state => state.productAnalysis.productAnalysisList,
      dataCount: state => state.productAnalysis.productAnalysisDataCount,
      historyData: state => state.productAnalysis.productAnalysisHistoryData,
      pageSize: state => state.productAnalysis.productAnalysisPageSize,
      row: state => state.productAnalysis.row,
      lostOrderList: state => state.order.lostOrderList,
      sellOrderList: state => state.order.sellOrderList,
      wantOrderList: state => state.order.wantOrderList,
      sellMoneyOrderList: state => state.order.sellMoneyOrderList,
      orderRateList: state => state.order.orderRateList,
      lostRateList: state => state.order.lostRateList
    })
  },
  methods: {
    ...mapActions([
      'getProductAnalysisList',
      'getLostOrderList',
      'getSellOrderList',
      'getWantOrderList',
      'getOrderRateList',
      'getLostRateList',
      'getSellMoneyOrderList',
      'getProductAnalysisCurrentRow',
      'editProductAnalysisData',
      'changeProductAnalysisPage',
      'changeProductAnalysisPageSize'
    ]),
    handleSubmit (playload) {
      this.loading = true
      // console.log(playload)
      this.playload = playload
      this.getProductAnalysisList({
        ...playload,
        rows: this.rows
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    // handleClick (id, row) {
    //   let newRow = {}
    //   this.id = id
    //   if (row) {
    //     newRow = Object.assign({}, row)
    //   }
    //   this.getSaleDrawerList({ uid: row.id ? row.id : -1 })
    //   this.getSaleCurrentRow(newRow)
    //   this.updateSaleDrawerState(true)
    // },
    handleClickTab (name) {
      // console.log(name)
      if (name === '列表') {
        this.loading = true
        this.getProductAnalysisList({
          // page: index,
          rows: this.rows,
          ...this.playload
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      }
      if (name === '成交量排名') {
        this.seriesName = '产品成交量'
        this.showPie = false
        this.getSellOrderList({
          rows: 10,
          page: 1
        }).then((res) => {
          if (res.data.state === '000') {
            this.showPie = true
          }
        })
      }
      if (name === '流失量排名') {
        this.seriesName = '产品流失量'
        this.showPie = false
        this.getLostOrderList({
          rows: 10,
          page: 1
        }).then((res) => {
          if (res.data.state === '000') {
            this.showPie = true
          }
        })
      }
      if (name === '意向量排名') {
        this.seriesName = '意向产品数量'
        this.showPie = false
        this.getWantOrderList({
          rows: 10,
          page: 1
        }).then((res) => {
          if (res.data.state === '000') {
            this.showPie = true
          }
        })
      }
      if (name === '成交率排名') {
        // this.seriesName = '销售业绩'
        // this.showPie = false
        this.showBar = false
        this.getOrderRateList({
          rows: 10,
          page: 1
        }).then((res) => {
          if (res.data.state === '000') {
            this.showBar = true
          }
        })
      }
      if (name === '流失率排名') {
        // this.seriesName = '销售业绩'
        // this.showPie = false
        this.showBar = false
        this.getLostRateList({
          rows: 10,
          page: 1
        }).then((res) => {
          if (res.data.state === '000') {
            this.showBar = true
          }
        })
      }
      if (name === '销售业绩排名') {
        this.seriesName = '销售业绩'
        this.showPie = false
        this.getSellMoneyOrderList({
          rows: 10,
          page: 1
        }).then((res) => {
          if (res.data.state === '000') {
            this.showPie = true
          }
        })
      }
    },
    handleSellOrder () {
      this.sellText = '爆品产品'
      this.showPie = false
      this.getSellOrderList({
        rows: 10,
        page: 1
      }).then((res) => {
        if (res.data.state === '000') {
          this.showPie = true
        }
      })
    },
    handleSellOrderDec () {
      // console.log(1)
      this.sellText = '销量低产品'
      this.showPie = false
      this.getSellOrderList({
        order: 'esc',
        rows: 10,
        page: 1
      }).then((res) => {
        if (res.data.state === '000') {
          this.showPie = true
        }
      })
    },
    handleSellMoneyOrder () {
      this.saleText = '销售业绩高'
      this.showPie = false
      this.getSellMoneyOrderList({
        rows: 10,
        page: 1
      }).then((res) => {
        if (res.data.state === '000') {
          this.showPie = true
        }
      })
    },
    handleSellMoneyOrderDec () {
      // console.log(1)
      this.saleText = '销售业绩低'
      this.showPie = false
      this.getSellMoneyOrderList({
        order: 'esc',
        rows: 10,
        page: 1
      }).then((res) => {
        if (res.data.state === '000') {
          this.showPie = true
        }
      })
    },
    changePage (index) {
      this.loading = true
      this.page = index
      this.changeProductAnalysisPage(index)
      this.getProductAnalysisList({
        page: index,
        rows: this.rows,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeProductAnalysisPageSize(value)
      this.changePage(this.page)
      this.getProductAnalysisList({
        rows: value,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    }
  },
  created () {
    this.getProductAnalysisList({}).then((res) => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
  }
}
</script>
